package com.stylefeng.guns.modular.pay.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.pay.model.MchInfo;
import com.stylefeng.guns.modular.pay.model.MchRouter;
import com.stylefeng.guns.modular.pay.model.PayChannel;
import com.stylefeng.guns.modular.pay.model.PayWay;
import com.stylefeng.guns.modular.pay.service.IMchInfoService;
import com.stylefeng.guns.modular.pay.service.IMchRouterService;
import com.stylefeng.guns.modular.pay.service.IPayChannelService;
import com.stylefeng.guns.modular.pay.service.IPayWayService;

/**
 * 商户路由控制器
 *
 * @author fengshuonan
 * @Date 2018-08-24 15:19:37
 */
@Controller
@RequestMapping("/mchRouter")
public class MchRouterController extends BaseController {

    private String PREFIX = "/pay/mchRouter/";

    @Autowired
    private IMchRouterService mchRouterService;
    @Autowired
    private IMchInfoService mchInfoService;
    @Autowired
    private IPayWayService payWayService;
    @Autowired
    private IPayChannelService payChannelService;

    /**
     * 跳转到商户路由首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "mchRouter.html";
    }

    /**
     * 跳转到添加商户路由
     */
    @RequestMapping("/mchRouter_add")
    public String mchRouterAdd(Model model) {
    	List<MchInfo> mchList = mchInfoService.selectList(new EntityWrapper<MchInfo>().eq("status", 1));
    	List<PayWay> wayList = payWayService.selectList(new EntityWrapper<PayWay>().eq("status", 1));
    	List<PayChannel> channelList = payChannelService.selectList(new EntityWrapper<PayChannel>().eq("status", 1));
    	model.addAttribute("mchList",mchList);
    	model.addAttribute("wayList",wayList);
    	model.addAttribute("channelList",channelList);
    	
        return PREFIX + "mchRouter_add.html";
    }

    /**
     * 跳转到修改商户路由
     */
    @RequestMapping("/mchRouter_update/{mchRouterId}")
    public String mchRouterUpdate(@PathVariable Integer mchRouterId, Model model) {
        MchRouter mchRouter = mchRouterService.selectById(mchRouterId);
        model.addAttribute("item",mchRouter);
        LogObjectHolder.me().set(mchRouter);
        return PREFIX + "mchRouter_edit.html";
    }

    /**
     * 获取商户路由列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return mchRouterService.selectList(null);
    }

    /**
     * 新增商户路由
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(MchRouter mchRouter) {
    	PayWay way = payWayService.selectOne(new EntityWrapper<PayWay>().eq("pay_way_code", mchRouter.getPayWayCode()));
    	PayChannel channel = payChannelService.selectOne(new EntityWrapper<PayChannel>().eq("pay_channel_code", mchRouter.getPayChannelCode()));
    	MchInfo mch = mchInfoService.selectOne(new EntityWrapper<MchInfo>().eq("mch_no", mchRouter.getMchNo()));
    	//验证
    	EntityWrapper<MchRouter> warpper= new EntityWrapper<MchRouter>();
    	Map<String,Object> map = new HashMap<>();
    	map.put("pay_way_code", mchRouter.getPayWayCode());
    	map.put("mch_no", mchRouter.getMchNo());
    	map.put("pay_channel_code", mchRouter.getPayChannelCode());
    	warpper.allEq(map);
    	int count = mchRouterService.selectCount(warpper);
    	if(count>0){
    		 throw new GunsException(BizExceptionEnum.ROUTER_EXIST);
    	}
    	mchRouter.setPayChannelName(channel.getPayChannelName());
    	mchRouter.setPayWayName( way.getPayWayName());
    	mchRouter.setStatus(1);
    	mchRouter.setCerateTime(new Date());
    	mchRouter.setMchName(mch.getMchName());
    	mchRouterService.insert(mchRouter);
        return SUCCESS_TIP;
    }

    /**
     * 删除商户路由
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer mchRouterId) {
        mchRouterService.deleteById(mchRouterId);
        return SUCCESS_TIP;
    }

    /**
     * 修改商户路由
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(MchRouter mchRouter) {
        mchRouterService.updateById(mchRouter);
        return SUCCESS_TIP;
    }

    /**
     * 商户路由详情
     */
    @RequestMapping(value = "/detail/{mchRouterId}")
    @ResponseBody
    public Object detail(@PathVariable("mchRouterId") Integer mchRouterId) {
        return mchRouterService.selectById(mchRouterId);
    }
}
